<?php
require '../config.php';
$filtro = $_REQUEST['filtro'];

$query = "select * from clientes where activo = '1' and (nombres LIKE '%".$filtro."%') OR (apellidos LIKE '%".$filtro."%') or concat(nombres,' ',apellidos) LIKE '%".$filtro."%' OR (cedula LIKE '%".$filtro."%')";

$resultset = mysql_query($query,$conexion);

if(!$resultset)
{
    mysql_close();
     echo "<legend>Sin Resultados</legend>";
}
elseif(!mysql_num_rows($resultset))
{
    mysql_close();
     echo "<legend>Sin Resultados</legend>";
}
else
{
		
			
			echo   "<table class='table table-bordered'>";
			echo   "<caption>Reporte en pantalla</caption>";
			echo   "<tr>";
			echo   "<th>Identificador</th><th>Nombre</th><th>Apellido</th><th>C&eacute;dula</th><th>Categor&iacute;a</th><th colspan='3'>Opciones</th>";
			echo   "</tr>";
			echo   "<tbody>";
			$aux=0;
			while($row = mysql_fetch_assoc($resultset))
			{
				if($aux==0){
					$color="#FFFFFF";
					$aux=1;
				}else{
					$color="#F9F9F9";
					$aux=0;
				}
				$categoria=retornaCategoria($row['categoria']);
				if($categoria=="RECURRENTE" or $categoria=="ADOPTA MINUTOS" )
					echo ("<tr bgcolor='".$color."'>");
				else 
					echo ("<tr class='danger'>");
				
				echo   "<td>".$row['idclientes']."</td>";
				echo   "<td>"._convert($row['nombres'])."</td>";
				echo   "<td>"._convert($row['apellidos'])."</td>";
				echo   "<td>".$row['cedula']."</td>";
				echo   "<td>".$categoria."</td>";
				
				$result2 = mysql_query("select p.insertar from permisos p,modulos m where p.idmodulo = m.idmodulos and p.idrol= '". $_SESSION['rol'] ."' and m.nombre = 'Clientes'",$conexion);
				echo   "<td><button type='button' class='btn btn-link btn-xs'  onclick='ver(". $row['idclientes'] .")'><span class='glyphicon glyphicon-eye-open'></span> Ver</button></td>";
				if(mysql_num_rows($result2)>0)
				{
					$row2 = mysql_fetch_assoc($result2);
					if($row2['insertar']== 1)
					{
						if($categoria=="RECURRENTE" or $categoria=="ADOPTA MINUTOS" )
							echo   "<td><button type='button' class='btn btn-link btn-xs'   onclick='regdonacion3(". $row['idclientes'] .")'><img src='../imagenes/glyphicons_037_coins.png' width='18' height='16' /> Registrar donaci&oacute;n</button> </td>";
						else
							echo   "<td></td>";
						echo   "<td><button type='button' class='btn btn-link btn-xs'  onclick='regllamada3(". $row['idclientes'] .")'><img src='../imagenes/glyphicons_442_earphone.png' width='18' height='16' /> Registrar llamada</button> </td>";
					}
					else {
						echo   "<td></td>";
						echo   "<td></td>";
					}
				
				}
								
				echo   "</tr>";
			}
			echo   "</tbody>";
			echo   "</table>";
			mysql_close();
		
}

function retornaCategoria($codigo){
	$resultset2 = mysql_query("select descripcion from categoriasclientes where idcategoriasclientes= ".$codigo);
	$row2 = mysql_fetch_assoc($resultset2);
	return $row2['descripcion'];
}

function _convert($content) {
	if(!mb_check_encoding($content, 'UTF-8')
	OR !($content === mb_convert_encoding(mb_convert_encoding($content, 'UTF-32', 'UTF-8' ), 'UTF-8', 'UTF-32'))) {

		$content = mb_convert_encoding($content, 'UTF-8');

		if (mb_check_encoding($content, 'UTF-8')) {
			// log('Converted to UTF-8');
		} else {
			// log('Could not converted to UTF-8');
		}
	}
	return $content;
}

?>